package com.amumu.netctoss.action;

import javax.annotation.Resource;

import com.amumu.netctoss.dao.AdminDao;
import com.amumu.netctoss.entity.Admin;
/**
 *用户个人信息 
 */
public class UserAction extends BaseAction {
	private Admin admin;
	@Resource
	private AdminDao dao;
	private String oldpwd;
	private String newpwd;
	

	//显示个人信息
	public String userInfo(){
		if(session.get("user")==null){
			return "tologin";
		}
		admin = (Admin) session.get("user");
		try {
			admin = dao.findById(admin.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	// 更新
	public String update(){
		try {
			dao.updateUserInfo(admin);
			ok = true;
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		}
		System.out.println("ok:"+ok);
		return SUCCESS;
	}
	
	//修改密码
	public String modiPwd(){
		
		if(newpwd==null || oldpwd==null) return SUCCESS;
		try {
			admin = (Admin) session.get("user");
			if(admin.getPassword().equals(oldpwd)){
				admin.setPassword(newpwd);
				dao.modifyPwd(admin);
				request.put("ok", 1);
			}else{
				request.put("ok", 0);
			}
		} catch (Exception e) {
			request.put("ok", 0);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	//getter and setter
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}
	
}
